public class NumArray2 {

    int[] nums;
    int[] dp;

    public NumArray2(int[] nums) {
        this.nums = nums;
        if (nums.length != 0) {
            int sum = 0;
            dp = new int[nums.length];
            for (int i = 0; i < nums.length; i++) {
                sum += nums[i];
                dp[i] = sum;
            }
        }
    }

    public void update(int index, int val) {
        int dis = val - nums[index];
        nums[index] = val;
        for (int i = index; i < nums.length; i++) {
            dp[i] += dis;
        }
    }

    public int sumRange(int left, int right) {
        return dp[right] - dp[left] + nums[left];
    }
}
